package com.wg.photo.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wg.photo.BaseActivity;
import com.wg.photo.R;
import com.wg.photo.utils.PickConfig;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PicturePreviewActivity extends BaseActivity {
    private Intent intent;
    private String mImgPath;
    private ImageView mIVpreview;
    private List<String> allImagePath;
    private List<String> selectImagePath;
    private CheckBox mCB;
    private boolean noChange = false;

    @Override
    public int getLayoutId() {
        initDatas();
        return R.layout.activity_picture_preview;
    }

    private void initDatas() {
        intent = getIntent();
        mImgPath = intent.getStringExtra(PickConfig.INTENT_IMG_PATH);
        allImagePath = (List<String>) getIntent().getSerializableExtra(PickConfig.INTENT_IMG_LIST);
        selectImagePath = (List<String>) getIntent().getSerializableExtra(PickConfig.INTENT_IMG_LIST_SELECT);
        if(selectImagePath == null){
            selectImagePath = new ArrayList<>();
        }
    }

    @Override
    public void initViews() {
        mIVpreview = (ImageView) findViewById(R.id.act_picture_preview_IV);
        mCB = (CheckBox) findViewById(R.id.act_picture_preview_CB);
    }

    @Override
    public void setListeners() {
        Glide.with(this).load(Uri.parse("file://"+mImgPath)).into(mIVpreview);
        mCB.setChecked(selectImagePath.contains(mImgPath));
        mCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!noChange){
                    if (isChecked){
                        if (!selectImagePath.contains(mImgPath)){
                            if (selectImagePath.size()<9){
                                selectImagePath.add(mImgPath);
                            }else {
                                noChange = true;
                                mCB.setChecked(!isChecked);
                            }
                        }
                    }else {
                        if (selectImagePath.contains(mImgPath)){
                            selectImagePath.remove(mImgPath);
                        }
                    }
                }
                noChange = false;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            setData();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void comeBack(View view) {
        setData();
        super.comeBack(view);
    }

    private void setData(){
        intent.putExtra(PickConfig.INTENT_IMG_LIST_SELECT, (Serializable) selectImagePath);
        setResult(RESULT_OK,intent);
        this.finish();
    }
}
